package com.lzp.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzp.goods.dao.BrandMapper;
import com.lzp.goods.pojo.Brand;
import com.lzp.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findAll() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public void add(Brand brand) {
        // brandMapper.insert() 表示的是插入，所有的字段都执行
        /*
            是选择性新增，比如不新增seq字段
            insert tb_brand into (id,name,image,letter)  values(?,?,?,?)
         */
        brandMapper.insertSelective(brand);
    }


    /*
     修改
     brand从前台页面上接收过来的参数，新数据
     brand1是表示根据id从数据库中查询到的，是老的数据
    */
    @Override
    public void update(Brand brand) {
        //1.先根据id查询实体
        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        if (brand1 != null) {
            //执行修改
            brandMapper.updateByPrimaryKeySelective(brand);
        } else {
            throw new RuntimeException("未查询到数据，不能修改");
        }
    }

    @Override
    public void deleteById(Integer id) {
        //1.先根据id查询实体
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand != null) {
            //执行删除
            brandMapper.deleteByPrimaryKey(id);
        } else {
            throw new RuntimeException("未查询到数据，不能删除");
        }
    }

    @Override
    public List<Brand> search(Brand brand) {
        //自定义类
        Example example = new Example(Brand.class);
        //获取条件构建器
        Example.Criteria criteria = example.createCriteria();
        if (brand.getName() != null) {
            //品牌名称参数
            criteria.andEqualTo("name", brand.getName());
        }
        if (brand.getLetter() != null) {
            //首字母参数
            criteria.andEqualTo("letter", brand.getLetter());
        }


            /*      另外一种
                andLike表示模糊查询，相当于sql语句中的like关键字
                参数1表示实体类中的属性名称
                参数2表示赋值，相当于 like "%华为%"
                "+earchMap.get("name")+"表示动态参数
             */
        //执行自定义查询
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    //分页
    @Override
    public Page<Brand> findByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<Brand> page1= (Page<Brand>) brandMapper.selectAll();

        return page1;
    }

    @Override
    public Page<Brand> findByPage2(int page, int size, Brand brand) {
        //分页操作
        PageHelper.startPage(page, size);
        //自定义类
        Example example = new Example(Brand.class);
        //获取条件构建器
        Example.Criteria criteria = example.createCriteria();
        if (brand.getName() != null) {
            //品牌名称参数
            criteria.andEqualTo("name", brand.getName());
        }
        if (brand.getLetter() != null) {
            //首字母参数
            criteria.andEqualTo("letter", brand.getLetter());
        }

        Page<Brand> page1 = (Page<Brand>) brandMapper.selectByExample(example);

        return page1;
    }


}
